package com.irdstudio.efp.flow.api.rest;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.flow.service.facade.BizEventChartInfoService;
import com.irdstudio.efp.flow.service.vo.BizEventChartInfoVO;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class BizEventChartInfoController extends AbstractController  {
	
	@Autowired
	@Qualifier("bizEventChartInfoService")
	private BizEventChartInfoService bizEventChartInfoService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/event/chart/infos", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizEventChartInfoVO>> queryBizEventChartInfoAll(
			@RequestBody BizEventChartInfoVO vo) {		
//		List<BizEventChartInfoVO> outputVo = this.executeQueryList("queryAll", bizEventChartInfoService, new Object[]{vo});
//		return getResponseData(outputVo);
		List<BizEventChartInfoVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = bizEventChartInfoService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = bizEventChartInfoService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = bizEventChartInfoService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = bizEventChartInfoService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/biz/event/chart/info", method=RequestMethod.GET)
	public @ResponseBody ResponseData<BizEventChartInfoVO> queryByPk(@RequestParam("bizFlowId") String bizFlowId,@RequestParam("bizEventId") String bizEventId) {		
		BizEventChartInfoVO inVo = new BizEventChartInfoVO();
				inVo.setBizFlowId(bizFlowId);
				inVo.setBizEventId(bizEventId);
		BizEventChartInfoVO outputVo = bizEventChartInfoService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param bizEventChartInfo
	 * @return
	 */
	@RequestMapping(value="/biz/event/chart/info", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody BizEventChartInfoVO inBizEventChartInfoVo) {		
		int outputVo = bizEventChartInfoService.deleteByPk(inBizEventChartInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inBizEventChartInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/event/chart/info", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody BizEventChartInfoVO inBizEventChartInfoVo) {		
		int outputVo = bizEventChartInfoService.updateByPk(inBizEventChartInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inBizEventChartInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/event/chart/info", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertBizEventChartInfo(@RequestBody BizEventChartInfoVO inBizEventChartInfoVo) {
		int outputVo = bizEventChartInfoService.insertBizEventChartInfo(inBizEventChartInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 保存数据
	 * @param inBizEventChartInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/event/chart/save", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> saveBizEventChartInfo(@RequestBody BizEventChartInfoVO inBizEventChartInfoVo) {
		setUserInfoToVO(inBizEventChartInfoVo);
		int outputVo = bizEventChartInfoService.saveBizEventChartInfo(inBizEventChartInfoVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 获取stencilsets
	 * @param inBizEventChartInfoVo
	 * @return
	 */
	@RequestMapping(value="/biz/event/chart/stencil-sets", method=RequestMethod.GET)
	public @ResponseBody ResponseData<String> getEventChartStencilSets() {
		String stencilsets = null;
		try {
			String jsonPath = Thread.currentThread().getContextClassLoader().getResource("/").getPath() + "chart/stencilsets.json";
			stencilsets = FileUtils.readFileToString(new File(jsonPath), "UTF-8");
		} catch (Exception e) {
			logger.error("发生异常!", e);
		}
		return getResponseData(stencilsets);
	}
}
